/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InvoiceStartPane.java
 *
 * Created on 24-jul-2010, 20:09:12
 */

package facturasapp;

import facturasapp.data.DataManager;
import facturasapp.data.StoreManager;
import facturasapp.events.EventManager;
import facturasapp.events.FacturaAdapter;
import facturasapp.events.FacturaEvent;
import java.awt.Color;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import storeDataModel.Invoice;
import storeDataModel.Product;

/**
 *
 * @author CiberXtrem
 */
public class InvoiceStartPane extends javax.swing.JPanel {

    /** Creates new form InvoiceStartPane */
    public InvoiceStartPane() {
        //initComponents();
        initCustomComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        matriculaLabel = new javax.swing.JLabel();
        matriculaField = new javax.swing.JTextField();
        modeloLabel = new javax.swing.JLabel();
        modeloField = new javax.swing.JTextField();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(facturasapp.FacturasApp.class).getContext().getResourceMap(InvoiceStartPane.class);
        matriculaLabel.setFont(resourceMap.getFont("matriculaLabel.font")); // NOI18N
        matriculaLabel.setText(resourceMap.getString("matriculaLabel.text")); // NOI18N
        matriculaLabel.setName("matriculaLabel"); // NOI18N

        matriculaField.setText(resourceMap.getString("matriculaField.text")); // NOI18N
        matriculaField.setName("matriculaField"); // NOI18N
        matriculaField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                matriculaFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                matriculaFieldFocusLost(evt);
            }
        });
        matriculaField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                matriculaFieldInputMethodTextChanged(evt);
            }
        });
        matriculaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                matriculaFieldKeyTyped(evt);
            }
        });

        modeloLabel.setFont(resourceMap.getFont("modeloLabel.font")); // NOI18N
        modeloLabel.setText(resourceMap.getString("modeloLabel.text")); // NOI18N
        modeloLabel.setName("modeloLabel"); // NOI18N

        modeloField.setName("modeloField"); // NOI18N
        modeloField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                modeloFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                modeloFieldFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(matriculaLabel)
                    .addComponent(matriculaField, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(modeloField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modeloLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(modeloLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modeloField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(matriculaLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(matriculaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
public void initCustomComponents(){
            matriculaLabel = new javax.swing.JLabel();
        matriculaField = new javax.swing.JTextField();
        modeloLabel = new javax.swing.JLabel();
        modeloField = new javax.swing.JTextField();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(facturasapp.FacturasApp.class).getContext().getResourceMap(InvoiceStartPane.class);
        matriculaLabel.setFont(resourceMap.getFont("matriculaLabel.font")); // NOI18N
        matriculaLabel.setText(resourceMap.getString("matriculaLabel.text")); // NOI18N
        matriculaLabel.setName("matriculaLabel"); // NOI18N

        matriculaField.setName("matriculaField"); // NOI18N
        matriculaField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                matriculaFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                matriculaFieldFocusLost(evt);
            }
        });
        matriculaField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                matriculaFieldInputMethodTextChanged(evt);
            }
        });
        matriculaField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                matriculaFieldKeyTyped(evt);
            }
        });

        modeloLabel.setFont(resourceMap.getFont("modeloLabel.font")); // NOI18N
        modeloLabel.setText(resourceMap.getString("modeloLabel.text")); // NOI18N
        modeloLabel.setName("modeloLabel"); // NOI18N

        modeloField.setName("modeloField"); // NOI18N
        modeloField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                modeloFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                modeloFieldFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(matriculaLabel)
                    .addComponent(matriculaField, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(modeloField, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modeloLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                //    )
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(modeloLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modeloField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(matriculaLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(matriculaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                //.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        )
        );

        EventManager.getInstance().addFacturaListener(new FacturaAdapter(){
             public void newInvoice(FacturaEvent e) {
                System.out.println("New invoice received");
                reset();
            }
        });
}

public void reset(){
    matriculaField.setText("");
    modeloField.setText("");
}

    private void matriculaFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_matriculaFieldKeyTyped
        //System.out.println("Key: " + ((JTextField)evt.getSource()).getText());
        //System.out.println("Key: " + matriculaField.getText());
    }//GEN-LAST:event_matriculaFieldKeyTyped

    private void matriculaFieldInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_matriculaFieldInputMethodTextChanged
        //System.out.println("Key: " + matriculaField.getText());
    }//GEN-LAST:event_matriculaFieldInputMethodTextChanged

    private void matriculaFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_matriculaFieldFocusLost

        matriculaField.setText(matriculaField.getText().toUpperCase());
        invoice = (Invoice)DataManager.getInstance().getDataObject("currentInvoice");

       String matricula = matriculaField.getText();
       if(matricula.trim().length() != 7){
           matriculaField.setBackground(Color.red);
       }
       else{
        invoice.setMatricula(matricula);
        matriculaField.setBackground(Color.white);
       }

       //Miramos a ver si ya existe en la base de datos la matricula y asignamos el modelo
       PersistenceManagerFactory pmf = StoreManager.get();
       PersistenceManager pm = pmf.getPersistenceManager();
       pm.currentTransaction().begin();

       Query query = pm.newQuery(Invoice.class, "matricula == p_matricula");
       query.setOrdering("id descenting");
       query.declareParameters("String p_matricula");
       List<Invoice> invoiceList = (List<Invoice>) query.execute(matriculaField.getText());
       if(invoiceList.size() > 0 ){
           modeloField.setText(invoiceList.get(0).getModelo());
       }
       pm.currentTransaction().commit();
       pm.close();
    }//GEN-LAST:event_matriculaFieldFocusLost

    private void modeloFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_modeloFieldFocusLost
        modeloField.setText(modeloField.getText().toUpperCase());
        String modelo= modeloField.getText();
        invoice.setModelo(modelo);
    }//GEN-LAST:event_modeloFieldFocusLost

    private void matriculaFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_matriculaFieldFocusGained
        matriculaField.selectAll();
    }//GEN-LAST:event_matriculaFieldFocusGained

    private void modeloFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_modeloFieldFocusGained
        modeloField.selectAll();
    }//GEN-LAST:event_modeloFieldFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField matriculaField;
    private javax.swing.JLabel matriculaLabel;
    private javax.swing.JTextField modeloField;
    private javax.swing.JLabel modeloLabel;
    // End of variables declaration//GEN-END:variables

    //Custom Data Variables
    Invoice invoice;
    //End Custom Data Variables
}
